<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?php
// check if the user is logged-in
include_once '../utils/includer.php';
includeFile('administrator/authentication.php');
isAuthorized();
//include 'authentication.php';

includeFileOnce("protected/database/MySqlDAO.php");
includeFileOnce("utils/mimeTypes.php");
includeFileOnce('utils/directories.php');
includeFile('protected/utils/FileSystemUtils.php');

if (isset($_GET['idc']))
    $idc = $_GET['idc'];
else
    $idc = 1;

$mySqlDAO = new MySqlDAO();
$parent_dir = $mySqlDAO->getParentDir($idc);

$backDir = $parent_dir;
?>

<html>
    <head>        
        <title>WebDoc</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="../styles/main.css">
        <link rel="stylesheet" type="text/css" href="../styles/admin.css">        
        <script type="text/javascript" src="../js/jquery-1.7.1.min.js" > </script>
        <script type="text/javascript" src="../js/jquery.validate.min.js" > </script>

        <script>
            $(document).ready(function() {
                $('span.actions').hide();
                
                $("div.contents-table-row").mouseover(function() {
                    var id = $(this).attr("id");
                    $(this).css('background-color', '#ffcc33');
                    //$(this).append('span.actions-'+id);
                    $('span#actions-'+id).show();
                }).mouseout(function(){
                    var id = $(this).attr("id");
                    $(this).css('background-color', '#fff');
                    $('span#actions-'+id).hide();
                });
            });
        </script>

    </head>
    <body>

        <div id="page" class="rounded">
            <div id="header">
                <div id="logo">Webdoc</div>
            </div>
            <div id="commandbar">
                <div id="menubar">
                    <ul>
                        <?php
                        if($idc!=1) {
                        ?>
                        <li><a href="index.php?idc=<?php echo $parent_dir; ?>" title="Back">
                                <img src="../images/back_arrow.png"/></a></li>
                        <?php } else { ?>
                       <li><a><img src="../images/back_arrow_inactive.png"/></a></li>
                        <?php } ?>
                        <li><a href="index.php?idc=1" title="Home">
                                <img src="../images/home.png"/></a></li>
                        <li><a href="createcategory.php?idc=<?php echo $idc; ?>" title="Add category">
                                <img src="../images/new_folder.png"/></a></li>
                        <li><a href="createfile.php?idc=<?php echo $idc; ?>" title="Add file">
                                <img src="../images/new_doc.png"/></a></li>
                        <li><a href="createlink.php?idc=<?php echo $idc; ?>" title="Add link">
                                <img src="../images/new_link.png"/></a></li>
                    </ul>
                </div>
                <div id="topbar">
                    <a href="../protected/ActionDispatcher.php?action=logout">Esci</a>
                </div>
                <hr/>
            </div>

            <div class="contents">

                <?php

                $categories = $mySqlDAO->getCategories($idc);
                $files = $mySqlDAO->getFiles($idc);
                $links = $mySqlDAO->getLinks($idc);

                $parent_dir = $idc;

                if (mysql_num_rows($categories) == 0 && mysql_num_rows($files) == 0 && mysql_num_rows($links) == 0) {
                    if ($idc != 1) { ?>
                       <a href='index.php?idc=<?php echo $backDir ?>'>.. Parent directory</a><br><br>
                        <?php }
                           
                            include_once ($_SERVER["DOCUMENT_ROOT"] . "/web-doc/protected/messages/it/messages.php");
                            
                            echo $messages['showcategory.category_empty'];
                        
                } else {
                    ?>

                    <div class="contents-table">                    
                        <div class="contents-table-header">
                            <div class="contents-table-caption-col0">&nbsp;</div>
                            <div class="contents-table-caption-col1">Titolo</div>
                            <div class="contents-table-caption-col2">Dimensione</div>
                            <div class="contents-table-caption-col3">Ultima modifica</div>
                        </div>
                        <hr/>

                        <?php if ($idc != 1) { ?>
                            <div class="contents-table-row">
                                <div class="contents-table-col">..</div>
                                <div class="contents-table-col">
                                    <a href='index.php?idc=<?php echo $backDir ?>'>
                                        Parent directory</a>
                                </div>
                                <div class="contents-table-col">&nbsp;</div>
                                <div class="contents-table-col">&nbsp;</div>
                            </div>
                            <hr/>
                        <?php } ?>

                        <?php
                        // Categories
                        while (list($idc, $name) = mysql_fetch_array($categories)) {
                            ?>
                            <div class="contents-table-row" id="category-id-<?php echo $idc ?>">
                                <div class="contents-table-col"><img src='../images/directory.png'> </div>
                                <div class="contents-table-col">
                                    <a href='index.php?idc=<?php echo $idc ?>'><?php echo $name ?></a>
                                    <span class="actions" id="actions-category-id-<?php echo $idc ?>">
                                        [<a href=deleteelement.php?idc=<?php echo $idc ?>&parent_dir=<?php echo $parent_dir ?>>Elimina</a>
                                        | 
                                        <a href=modifycategory.php?idc=<?php echo $idc ?>&parent_dir=<?php echo $parent_dir ?>&name=<?php echo $name ?>>Modifica</a>]
                                    </span>
                                </div>
                                <div class="contents-table-col">&nbsp;</div>
                                <div class="contents-table-col">&nbsp;</div>
                            </div>
                            <hr/>
                            <?php
                        }

                        // Files
                        while (list($idf, $title, $fileName, $type, $size, $date) = mysql_fetch_array($files)) {
                            $image = getImage($type);

                            $path = $mySqlDAO->getCategoryPath($idc) . "/" . $fileName;
                            $href = "http://" . getHostBaseDir() . "/" . getFileSystemDir() . $path;
                            ?>                              
                            <div class="contents-table-row" id="file-id-<?php echo $idf ?>">
                                <div class="contents-table-col"><img src='<?php echo $image ?>'></div>
                                <div class="contents-table-col">
                                    <a href='<?php echo $href ?>'><?php echo $title ?></a>
                                    <span class="actions" id="actions-file-id-<?php echo $idf ?>">
                                        [<a href=deleteelement.php?idf=<?php echo $idf ?>&parent_dir=<?php echo $parent_dir ?>>Elimina</a>
                                        | 
                                        <a href=modifyfile.php?idf=<?php echo $idf ?>&parent_dir=<?php echo $parent_dir ?>&name=<?php echo $title ?>>Modifica</a>]
                                    </span>
                                </div>
                                <div class="contents-table-col"><?php echo FileSystemUtils::getFileSize($size) ?></div>
                                <div class="contents-table-col"><?php echo date("d/m/Y g:i A", strtotime($date)) ?></div>
                            </div>
                            <hr/>                            
                            <?php
                        }

                        // Links
                        while (list($idl, $title, $url) = mysql_fetch_array($links)) {
                            ?>
                            <div class="contents-table-row" id="link-id-<?php echo $idl ?>">
                                <div class="contents-table-col"><img src='../images/url.png'></div>
                                <div class="contents-table-col">
                                    <a href='<?php echo $url ?>' target='_NEW'><?php echo $title ?></a>
                                    <span class="actions" id="actions-link-id-<?php echo $idl ?>">
                                        [<a href=deleteelement.php?idl=<?php echo $idl ?>&parent_dir=<?php echo $parent_dir ?>>Elimina</a>
                                        | 
                                        <a href=modifylink.php?idl=<?php echo $idl ?>&parent_dir=<?php echo $parent_dir ?>>Modifica</a>]
                                    </span>
                                </div>
                                <div class="contents-table-col">&nbsp;</div>
                                <div class="contents-table-col">&nbsp;</div>
                            </div>
                            <hr/>
                            <?php
                        }
                    }
                    ?>
                </div>
            </div>
        </div>


    </body>
</html>
